package com.ztstkj.lzscale.adpters

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.ztstkj.lzscale.R


class CategroyAdapter(private var datas: List<CategoryItem>) :
    RecyclerView.Adapter<CategroyAdapter.ItemViewHolder>() {
    lateinit var context: Context
    private var selectedPoisiton:Int= RecyclerView.NO_POSITION
    var itemClickListener: ICategoryItemClickListener? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        context = parent.context
        val v = LayoutInflater.from(parent.context)
            .inflate(R.layout.activity_category_item, parent, false)
        return ItemViewHolder(v)
    }

    @SuppressLint("UseCompatLoadingForDrawables", "NotifyDataSetChanged")
    override fun onBindViewHolder(holder: ItemViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val bean = datas[position]
        val resId = context.resources.getIdentifier(bean.icon, "drawable", context.packageName)
        val categoryImage = context.resources.getDrawable(resId, null)
        holder.categoryIcon.setImageDrawable(categoryImage)
        holder.categoryName.text = bean.name
        holder.onSelect(position==selectedPoisiton)
        holder.categorySelectedLinear.setOnClickListener {
            selectedPoisiton=position
            notifyDataSetChanged()
            itemClickListener?.onItemClickListener(position)
        }
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    @SuppressLint("NotifyDataSetChanged")
    fun selectReset(position: Int?=null){
        if (position == null) {
            selectedPoisiton= RecyclerView.NO_POSITION
        } else {
            selectedPoisiton=position
        }
        notifyDataSetChanged()
    }

    inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val categoryIcon: ImageView = view.findViewById(R.id.recyclerCategoryIcon)
        val categoryName: TextView = view.findViewById(R.id.recyclerCategoryName)
        private val categorySelectedImage: ImageView = view.findViewById(R.id.recyclerCategorySelected)
        val categorySelectedLinear: LinearLayout = view.findViewById(R.id.recyclerCategoryLinear)
        @SuppressLint("UseCompatLoadingForDrawables")
        fun onSelect(isSelected: Boolean) {
            if (isSelected) {
                categorySelectedImage.visibility = View.VISIBLE
                categorySelectedLinear.background =
                    context.getDrawable(R.drawable.main_category_bg_seleted)
            } else {
                categorySelectedImage.visibility = View.GONE
                categorySelectedLinear.background =
                    context.getDrawable(R.drawable.main_category_bg)
            }
        }
    }
    //自定义接口
    interface ICategoryItemClickListener {
        fun onItemClickListener(position: Int)
    }
}